package cqueue;

import cqueue.firing.LocalFireServiceStub;
import cqueue.posting.LocalPostServiceStub;
import cqueue.posting.TcpPostServiceSkel;
import cqueue.posting.TcpPostServiceStub;

/**
 * The Producer in the FactoryMethod pattern. Use this
 * class to create cqueues. 
 * 
 * 
 * @author Kristopher L. Kalish, James Madison University
 * @version 1 - March 29, 2008
 *
 */
public class EventQueueFactory
{
	/**
	 * Creates a normally functioning event queue where all action happens
	 * locally.
	 */
	public static ListenerEventQueue createLocalListenerQueue()
	{
		ListenerEventQueue   queue;
		LocalFireServiceStub fire;
		LocalPostServiceStub post;
		
		queue = new ListenerEventQueue();
		
		fire = new LocalFireServiceStub(queue.listeners);
		post = new LocalPostServiceStub(queue.events);

		queue.addFireService(fire);
		queue.addPostService(post);
		
		return queue;
	}
	
	/**
	 * Creates a "master" event queue where events are fired and posted locally,
	 * but also posted to remote destinations.
	 */
	public static ListenerEventQueue createMasterListenerQueue(int listenPort)
	{
		ListenerEventQueue queue;
		TcpPostServiceStub post;
		
		queue = createLocalListenerQueue();
		
		post = new TcpPostServiceStub(listenPort);
		
		queue.addPostService(post);
		
		return queue;
	}
	
	/**
	 * Creates a "node" event queue where events are fired and posted locally,
	 * but also posts events received over tcp.
	 */
	public static ListenerEventQueue createNodeListenerQueue(String host, int port)
	{
		ListenerEventQueue queue;
		TcpPostServiceSkel post;
		
		queue = createLocalListenerQueue();
		
		post = new TcpPostServiceSkel(queue.events, queue.lock, host, port);
		
		queue.addPostService(post);
		
		return queue;
	}
}
